package com.yulongtian.oneProblemEveryDay.month202212;

import java.util.*;

/**
 * https://leetcode.cn/problems/two-out-of-three/
 * 可
 *
 * @author yulongTian
 * @create 2022-12-29 21:09
 */
public class Test13 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2, 2};
        int[] nums2 = {4, 3, 3};
        int[] nums3 = {5};

        System.out.println(twoOutOfThree(nums1, nums2, nums3));

    }


    public static List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {

        HashMap<Integer, List<Integer>> map = new HashMap<>();

        for (int i : nums1) {
            if (!map.containsKey(i)) {
                List<Integer> temp = new ArrayList<>();
                temp.add(0);
                map.put(i, temp);
            }
        }

        for (int i : nums2) {
            if (!map.containsKey(i)) {
                List<Integer> temp = new ArrayList<>();
                temp.add(1);
                map.put(i, temp);
            } else {
                List<Integer> integers = map.get(i);
                if (!integers.contains(1)) {
                    map.get(i).add(1);
                }
            }
        }

        for (int i : nums3) {
            if (!map.containsKey(i)) {
                List<Integer> temp = new ArrayList<>();
                temp.add(2);
                map.put(i, temp);
            } else {
                List<Integer> integers = map.get(i);
                if (!integers.contains(2)) {
                    map.get(i).add(2);
                }
            }
        }

        int[][] arr = new int[map.size()][2];

        int index = 0;
        for (Integer integer : map.keySet()) {
            arr[index][0] = integer;
            arr[index][1] = map.get(integer).size();
            index++;
        }

        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o2[1] - o1[1];
            }
        });

        if (arr[0][1] < 2) {
            return new ArrayList<>();
        }

        List<Integer> ans = new ArrayList<>();
        for (int[] ints : arr) {
            if (ints[1] >= 2) {
                ans.add(ints[0]);
            }
        }


        return ans;
    }


}
